<template>
  <div class="audit-form">
    <Form :model="auditForm" :rules="auditRules" :label-width="70">
      <FormItem label="是否同意">
        <RadioGroup v-model="auditForm.processType">
          <Radio :label="1" border>同意</Radio>
          <Radio :label="0" border>拒绝</Radio>
        </RadioGroup>
      </FormItem>
      <Row>
        <Col span="16">
          <FormItem label="审批意见">
            <Input type="textarea" v-model="auditForm.opinion" :rows="3" placeholder="请输入审批意见" :maxlength="255"></Input>
          </FormItem>
        </Col>
      </Row>
    </Form>
    <div class="audit-btn">
      <Button @click="cancelAudit">
        取消
      </Button>
      <Button type="warning" @click="transfer">
        转办
      </Button>
      <Button type="info" @click="doAudit">
        确定
      </Button>
    </div>
    <Modal
      v-model="modal.show"
      :title="modal.title"
      :width="modal.width"
      scrollable
      @on-cancel="closeModal"
      class="modal-area"
      footer-hide
    >
      <check-user v-if="modal.type==='user'" :isSingle="true" @close-modal="closeModal" @sure-back="checkedUser"></check-user>
    </Modal>
  </div>
</template>

<script>
import * as API from "@/api/bpmn-api";
import checkUser from '_c/sys-components/check-user'

export default {
  name: "audit-form",
  components:{checkUser},
  props: {
    variables: {
      type: Object,
      default() {
        return {

        }
      }
    },
    processObject: {
      type: Object,
      default() {
        return {

        }
      }
    }
  },
  data() {
    return {
      modal: {
        show: false,
        title: "",
        type: "",
        width: 300,
      },
      auditForm: {
        processType: 1,
        opinion: "",
      },
      auditRules: {},
    }
  },
  methods: {
    //点击取消按钮
    cancelAudit() {
      this.auditForm = {
        processType: 1,
        opinion: ""
      }
      this.$emit('close-drawer')
    },

    //点击转办按钮
    transfer() {
      this.modal.show=true
      this.modal.width=500
      this.modal.type="user"
      this.modal.title="选择审核人"
    },


    //关闭弹窗
    closeModal() {
      let cleanModal = {
        show: false,
        title: "",
        type: "",
        width: 300
      }
      this.modal = Object.assign({}, cleanModal)
    },

    //获取勾选的人员
    checkedUser(user){
      this.$Modal.confirm({
        title: '转办确认',
        content: '<p>是否确认将任务转交由【'+user.name+'】审核？</p>',
        onOk: () => {
          API.transferTask({
            processInstanceId: this.processObject.processInstanceId,
            variables: this.variables,
            assignee:user.id
          }).then(res => {
            if (res.code === "0") {
              this.$Message.success("转办任务成功！")
              this.closeModal()
              this.cancelAudit()
              this.$emit("fresh-data")
            }
          })
        },
        onCancel: () => {
        }
      });
    },



    //点击确定按钮
    doAudit() {
      API.auditTask({
        processInstanceId: this.processObject.processInstanceId,
        definitionKey: this.processObject.processDefinitionKey,
        variables: this.variables,
        processDescription: this.auditForm.opinion,
        processType: this.auditForm.processType,
      }).then(res => {
        if (res.code === "0") {
          this.$Message.success("审核成功！")
          this.cancelAudit()
          this.$emit("fresh-data")
        }
      })
    },
  }
}
</script>

<style lang="less" scoped>
.audit-form {
  .audit-btn {
    bottom: 20px;
    position: fixed;
    height: 50px;
    text-align: center;
    width: 100%;

    button {
      margin-left: 5px;
    }
  }

}
</style>
